<?php session_start();
//ini_set ('register_globals', '1');
//ini_set ('display_errors', '1');
ini_set('display_errors','off');

$key = 'ABQIAAAAREkopdB2DthaLKWLXUcWgxQoU8h1rNpuPlavOloCdSisrcvaLRTN2drNPlHMzM4OcpCreBqE1PmA8g';
if ($_SERVER['SERVER_NAME']=='dyasar.brinkster.net') $key = 'ABQIAAAAREkopdB2DthaLKWLXUcWgxTCuT1TvZZx_JRCLW8V8zEkwsB-uRTH7JlFGO17NKQMVpvSgBDRVEWlzg';

$_SESSION['google_key'] = $key;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Display Craigslist stuff on the map...</title>
    <script src="http://maps.google.com/maps?file=api&v=2&key=<?=$_SESSION['google_key'];?>"
      type="text/javascript"></script>

		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
		<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js" type="text/javascript"></script>
		<script type="text/javascript" src="js/jq.timers.js"></script>
		<script type="text/javascript" src="js/jq.form.js"></script>
		
    <script type="text/javascript">

    //<![CDATA[

		var geocoder;
		var map;
		var counter;	
		var address=new Array();
		var title=new Array();
		var body=new Array();
		var marker = new Array();
		var window_height;
		var window_width;
   // On page load, call this function

	function createMarker(point,html) {
	    var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
       return marker;
	}
	


   function load()
   {
      // Create new map object
      map = new GMap2(document.getElementById("map"));
			map.setUIToDefault();
			map.setCenter(new GLatLng(40.80601358609009, -73.9463996887207), 13);
      

      // Create new geocoding object
      geocoder = new GClientGeocoder();

	}




	function get_ad_details(ad){
		var ad_details;
		$.getJSON("get_cr.php?l="+ad['link'], function(data){
				if ( data != null ) {
				ad_details = data;
					if(data['latitude']!=''){
						lat = data['latitude'];
						lon = data['longitude'];				
						point = new GLatLng(lat,lon);
						html = ad['title']+ "<br>" + data['location']+ "<br><a href='"+ad['link']+"' target='_blank'>Go to the listing in a new window...</a>";
						marker = createMarker(point,html)			
						map.addOverlay(marker);				
					}
				}
			});
	}



 //]]>
    </script>
   <script type="text/javascript"> 
        // wait for the DOM to be loaded 
        $(document).ready(function() {
			
			var options = { 
			        //target:        '#output1',   // target element(s) to be updated with server response 
			        beforeSubmit:  showRequest,  // pre-submit callback 
			        success:       showResponse,  // post-submit callback 
			        dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
			    };	 
            $('#cr_form').ajaxForm(options); 
			resize_elements();		
		});
		
		function kd_loading(msg){
			if(msg){
				$("#fetching").html(msg);
				w = $("#fetching").outerWidth();
				r = ( window_width - w )/2;
				$("#fetching").css("right",r);
				$("#fetching").fadeIn(200);
			}else{
				$("#fetching").fadeOut(400);
			}
		}
		
		window.onresize = function(){
			resize_elements();
			
			
		}

		function resize_elements(){
			window_width = $(window).width();
			window_height = $(window).height();
			a = $('#cr_header').outerHeight();
			$('#map').css('height',window_height-a);			
		}
		var queryString;
		// pre-submit callback 
		function showRequest(formData, jqForm, options) { 
		    // formData is an array; here we use $.param to convert it to a string to display it 
		    // but the form plugin does this for you automatically when it submits the data 
		    var queryString = $.param(formData); 

		    // jqForm is a jQuery object encapsulating the form element.  To access the 
		    // DOM element for the form do this: 
		    // var formElement = jqForm[0]; 

		    //alert('About to submit: \n\n' + queryString); 
			$(this).stopTime('ss');
		    // here we could return false to prevent the form from being submitted; 
		    // returning anything other than false will allow the form submit to continue 
		    return true; 
		} 
	

		// post-submit callback 
		function showResponse(responseText, statusText)  { 
			
			var total;
			var remaining;
			$(this).everyTime(1000,'ss', function(x) { 
				total = parseInt(responseText[x]['total_ads']);
				remaining = total -(x+2);
				kd_loading('Total results remaining: ' + remaining);
				get_ad_details(responseText[x]);
				if(remaining.substr(-2)=='00'){
					kd_loading('Total results remaining: ' + remaining + '<span onclick="continue_fetching()"> Continue fetching?</span>');
				} 
				 
			},total-2);

		}

		function continue_fetching(s){
			console.log(queryString);
			
			qA = queryString.split("&s=",1);
			s = qA[1]+100;
			
			queryString = qA[0]+'&s='+s;
			//s=100;
			
			
			 $.getJSON("get_cr_listing.php?"+queryString, function(data){showResponse(data);});
			
		}

    </script>


<style>
	
	body, html { 
		margin:0;
		padding:0;
		
		}
	.h10 {
		position: fixed;
		height:auto;
		border: 2px solid #e9b96e;
		border-bottom-width: 0px;
		bottom: 0;
		right: 0;
		line-height: 27px;
		background: #fce94f;
		font-size:18px;
		padding: 0 15px;
		color: #2e3436;
		text-align: center;
		white-space:normal;
		z-index: 2500;
		box-shadow: #000 0 0 10px;
		-webkit-box-shadow: #000 0 0 10px;
	}
	
</style>
  </head>

  <body onload="load()" onunload="GUnload()">
 	<div id='cr_header'><? include('providers/craigslist_header.php'); ?></div>
    <div id="map"></div>
	<div id="fetching" class='h10'></div>
	<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-6520910-7");
pageTracker._trackPageview();
} catch(err) {}</script>
  </body>
</html>
